//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    titleBarHeight: '',
    tabs: [],
    activeTab: 0,
    list: [],
    lists: [],
    showLoading: true,
    searchData: '',
    pushOver: false,
    offset: 0,
    alpha: '',
    countryList: [],
    countryLists: [],
    countryIndex: 0,
    countryTabData: [],
    countryTabDatas: [],
    typeId: "",
    type: "",
    wenid: [],
    textdis: '',
    more: '',
    distilleryid: "", //酒柜传的id
    showView: false, //盒子弹框的显示隐藏
    showColor: false, //筛选的文字颜色变化
    showpin: '', //威士忌头部弹框筛选弹框传参
    showpinItem: false, //威士忌头部弹框筛选弹框传参
    showpinItemOne: false, //威士忌头部弹框筛选弹框传参
    showpinItemTow: false, //威士忌头部弹框筛选弹框传参
    showpinItemThree: false, //威士忌头部弹框筛选弹框传参
    showpinItemFor: false, //威士忌头部弹框筛选弹框传参
    endnumber: '',
    endyaers: '',
    endyaer: '',
    startnumber: '',
    startyaers: '',
    startyaer: '',
    index: 0, //起始页
    des: 20, //结束
    sort_proofbl: false,
    sort_distillation_timebl: false,
    sort_proof: "", //酒精排序赋值
    sort_distillation_time: "", //年份排序赋值
    searChdata: [],
    searChindex: '', //上弹框index
    searChiname: '', //上弹框搜索名字
    searChibrand: "",
    searChlist: "",
    searChlists: "",
    target: '',
    seanames: "酒厂",
    seaname: "品牌",
    xia: 1,
    searChheader: "",
    talk: '',
    isfouce: true,

  },
  onLoad: function (options) {
    if (options.search != undefined) {
      // console.log(1111)
      this.getDatas(options.search, this.data.typeId)

    } else {
      console.log(2222)

    }

    if (options.typeId == 'tt') {

      var that = this;
      console.log(options)
      that.setData({
        type: options.typeId,
        typeId: options.typeId
      })
      that.getData(0)

      that.setData({
        activeTab: 0
      })



      wx.getSystemInfo({
        success: function (res) {
          console.log(res, 'hahahahahhaha')
          that.setData({
            titleBarHeight: res.statusBarHeight + 46
          })
        }
      })

      const titles = ['威士忌', '品牌', '酒厂', '装瓶商']
      const tabs = titles.map(item => ({
        title: item
      }))
      console.log(tabs)
      this.setData({
        tabs
      })

    } else {

      console.log(options.name)
      var that = this;

      that.setData({
        typeId: options.typeId
      })
      that.getData(options.typeId)

      that.setData({
        activeTab: options.typeId
      })

      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            titleBarHeight: res.statusBarHeight + 46
          })
        }
      })

      const titles = ['威士忌', '品牌', '酒厂', '装瓶商']
      const tabs = titles.map(item => ({
        title: item
      }))
      console.log(tabs)
      this.setData({
        tabs
      })
    }


  },
  onTabCLick(e) {
    const index = e.detail.index
    console.log(index)
    //切换栏目，重置数据
    this.setData({
      activeTab: index,
      showLoading: true,
      list: []
    })
    setTimeout((item) => {
      this.getData(index)
    }, 500)
  },
  scrollCountry(e) {
    console.log(e)
  },

  onChange(e) {
    // const index = e.detail.index
    // this.setData({activeTab: index})
  },
  aaa() {},
  navBack() {
    wx.navigateBack({
      delta: 1
    })
  },
  //酒厂数据列表
  getsearChdata() {
    let that = this
    app.request({
      url: "/list-distillery-v2",
      data: {
        'offset': that.data.index,
        "num": that.data.des,
        "target": that.data.target,
      },
      method: "post",
      success: function (res) {
        console.log(res.data.records)
        that.setData({
          searChdata: res.data.records
        })
      }
    })
  },
  getsearChdatas() {
    let that = this
    app.request({
      url: "/list-brand-v2",
      data: {
        'offset': that.data.index,
        "num": that.data.des,
        "target": that.data.target,
      },
      method: "post",
      success: function (res) {
        console.log(res.data.list)
        that.setData({
          searChdata: res.data.records
        })
      }
    })
  },
  getDatas(data, typeId) {

    let that = this
    if (that.data.pushOver) return
    let datas = {
      target: that.data.searChheader,
      offset: that.data.index,
      num: that.data.des,

    }
    let typeArr = ['whisky', 'brand', 'distillery', 'business']
    app.request({
      url: '/whisky' + '/search-list',
      data: datas,
      success: function (res) {
        that.setData({
          list: this.data.list,
          index: res.data.offset
        })
      }
    })
  },

  getData(numbers) {
    let that = this
    console.log(wx.getStorageSync('uid'))
    app.request({
      url: "/selectWhiskyList",

      data: {
        'offset': this.data.index,
        "num": 20,
        "bottled_time_low": this.data.startyaers,
        "bottled_time_high": this.data.endyaers,
        "stated_year_low": this.data.startyaer,
        "stated_year_high": this.data.endyaer,
        "score_low": this.data.startnumber,
        "score_high": this.data.endnumber,
        "sort_proof": this.data.sort_proof,
        "sort_stated_age": this.data.sort_distillation_time,
        "distillery": this.data.searChiname,
        "brand": this.data.searChibrand,
        "target": this.data.searChheader,
      },
      method: 'post',
      success: function (res) {
        console.log(res)
        that.data.list.push(res.data.records)
        that.setData({
          list: that.data.list,
          index: res.data.offset,
          more: res.data.more,
          textdis: res.data.more
        })
      }
    })
  },
  //关闭弹框
  clasepin() {
    this.setData({
      showpinItem: false,
      showpinItemOne: false,
      showpinItemTow: false

    })
  },
  seachjiuchang(e) {
    console.log(e.detail.value)
    this.setData({
      target: e.detail.value
    })
    this.getsearChdata()
  },
  searChlist() {
    this.setData({
      searChlist: "",
      seanames: '酒厂',
      searChiname: '',
      searChibrand: '',
      showpinItem: false,
      showpinItemOne: false
    })
    console.log('jhdjddhdhdh')
    this.getData()
  },
  searChlists() {
    this.setData({
      list: [],
      searChlists: "",
      seaname: '品牌',
      searChibrand: '',
      searChibrand: '',
      showpinItem: false,
      showpinItemTow: false

    })
    console.log('jhdjddhdhdh')
    this.getData()
  },
  itemMinseach(e) {
    console.log(e)
    this.setData({
      list: []
    })
    if (this.data.showpin == 1) {
      this.setData({
        searChlist: e.currentTarget.dataset.item,
        searChindex: e.currentTarget.dataset.index,
        searChiname: e.currentTarget.dataset.item.name,
        seanames: e.currentTarget.dataset.names,
      })
    } else {
      this.setData({
        searChlists: e.currentTarget.dataset.item,
        searChindex: e.currentTarget.dataset.index,
        searChibrand: e.currentTarget.dataset.item.name,
        seaname: e.currentTarget.dataset.names,

      })
    }
    console.log(this.data.searChlist)
    this.getData()
    this.setData({
      showpinItem: false,
      showpinItemOne: false,
      showpinItemTow: false

    })
  },
  //头部的三个弹框
  showpin(e) {
    this.setData({
      showpin: e.currentTarget.dataset.typeid,
    })

    if (this.data.showpin == 1) {
      this.getsearChdata()
    }
    if (this.data.showpin == 1) {
      this.setData({
        showpinItemOne: !this.data.showpinItemOne,
        showpinItemTow: false,
        showpinItemFor: false,
        showpinItemThree: false,
        searChlists: '',
        searChlist: this.data.seaname,


      })
    }
    // searChlist
    if (this.data.showpin == 2) {
      this.getsearChdatas()
      this.setData({
        searChlist: '',
        showpinItemFor: false,
        showpinItemThree: false,
        showpinItemOne: false,
        showpinItemTow: !this.data.showpinItemTow,
        searChlist: '',
        searChlists: this.data.seanames,

      })

    }


    if (this.data.showpinItemOne || this.data.showpinItemTow || this.data.showpinItemThree || this.data.showpinItemFor == true) {
      this.setData({
        showpinItem: true,
      })
    } else {
      this.setData({
        showpinItem: false,
      })
    }
  },
  //筛选弹框
  searChitem() {
    this.setData({
      showView: !this.data.showView,
      showColor: !this.data.showColor
    })
    console.log(this.data.color)
  },
  searChitems() {
    this.setData({
      showView: !this.data.showView,
      showColor: !this.data.showColor
    })
  },
  goSearch(e) {
    if (wx.pageScrollTo) { //判断这个方法是否可用
      wx.pageScrollTo({
        scrollTop: 0
      })
    }
    this.setData({
      xia: "",
      searChheader: e.detail.value,
      index: 0,
      des: 50,
      list: []
    })
    this.getData(this.data.typeId)

    // let that = this
    // let data = {
    //   typeId: that.data.activeTab,
    //   data: e.detail.value
    // }
    // console.log(data)
    // wx.navigateTo({
    //   url: '/pages/search/index',
    //   success: function(res) {
    //     // 通过eventChannel向被打开页面传送数据
    //     res.eventChannel.emit('acceptDataFromOpenerPage', data)
    //   }
    // })
  },

  getstartyaers(e) {
    this.setData({
      startyaers: e.detail.value
    })
  },
  getndyaers(e) {
    this.setData({
      endyaers: e.detail.value
    })
  },
  getstartyaer(e) {
    this.setData({
      startyaer: e.detail.value
    })
  },
  getndyaer(e) {
    this.setData({
      endyaer: e.detail.value
    })
  },
  getstartnumber(e) {
    this.setData({
      startnumber: e.detail.value
    })
  },
  getendnumber(e) {
    this.setData({
      endnumber: e.detail.value
    })
  },
  //点击筛选
  filtrate() {
    if (wx.pageScrollTo) { //判断这个方法是否可用
      wx.pageScrollTo({
        scrollTop: 0
      })
    }
    this.setData({
      list: [],
      index: 0,
      des: 50,
    })
    this.getData()


  },
  //酒精度排序
  alcohol() {
    if (wx.pageScrollTo) { //判断这个方法是否可用
      wx.pageScrollTo({
        scrollTop: 0
      })
    }
    this.setData({
      sort_proofbl: !this.data.sort_proofbl,
      sort_distillation_timebl: false,
      index: 0,
      des: 50
    })
    if (this.data.sort_proofbl == true) {
      this.setData({
        list: [],
        sort_proof: "ASC",
        sort_distillation_time: '',
        index: 0,
        des: 50

      })
    } else {
      this.setData({
        list: [],
        sort_proof: "DESC",
        sort_distillation_time: '',
        index: 0,
        des: 50

      })
    }
    this.getData()
  },
  //年份排序
  particular() {
    if (wx.pageScrollTo) { //判断这个方法是否可用
      wx.pageScrollTo({
        scrollTop: 0
      })
    }
    this.setData({
      sort_distillation_timebl: !this.data.sort_distillation_timebl,
      list: [],

      sort_proofbl: false,
      index: 0,
      des: 50
    })
    if (this.data.sort_distillation_timebl == true) {
      this.setData({
        list: [],
        sort_proof: "",
        sort_distillation_time: 'ASC',
        index: 0,
        des: 50
      })
    } else {
      this.setData({
        list: [],
        sort_distillation_time: "DESC",
        sort_proof: "",
        index: 0,
        des: 50
      })
    }
    this.getData()
  },
  //文本内容
  talk: function (e) {
    this.setData({
      isfouce: true,
      talk: e.detail.value
    })
  },
  isfouce() {
    this.setData({
      isfouce: false
    })
  },
  //点击重置按钮
  clearInput() {
    this.setData({
      endnumber: '',
      startnumber: '',
      endyaer: '',
      endyaers: '',
      startyaer: '',
      startyaers: '',
    })
    this.getData(this.data.typeId)
  },
  goDes(e) {
    // wx.navigateTo({
    //   url: '/pages/works/index?id=' + e.currentTarget.dataset.item.wineid,
    // })
    // //判断进入页面的路径
    // if (this.data.type == 'tt') {
    //   let index = e.currentTarget.dataset.index;
    //   let parentindex = e.currentTarget.dataset.parentindex;
    //   this.data.wenid.push(e.currentTarget.dataset.item.wineid)
    //   this.data.list[parentindex].datas[index].active = true;
    //   this.setData({
    //     list: this.data.list,
    //     // ['list[' + index + '].datas[' + parentindex + '].active']: true,
    //     distilleryid: e.currentTarget.dataset.item.wineid,
    //     wenid: this.data.wenid
    //   })
    //   console.log(this.data.wenid)
    // } else {
    //   let data = e.currentTarget.dataset.item
    //   console.log(data)
    //   data.typeId = this.data.activeTab
    //   if (data.typeId >= 1) {
    //     // wx.setStorageSync('data', data)
    //     console.log(JSON.stringify(data))
    //     wx.navigateTo({
    //       url: '/pages/details/index?id=' + JSON.stringify(data),
    //       success: function (res) {
    //         // 通过eventChannel向被打开页面传送数据
    //         // res.eventChannel.emit('acceptDataFromOpenerPage', data)
    //       }
    //     })
    //   } else {
    //     console.log(e.currentTarget.dataset.item.wineid)


    //   }
    // }

    if (this.data.type == 'tt') {
      console.log(e)
      let index = e.currentTarget.dataset.index;
      let indexs = e.currentTarget.dataset.idindex
      // let parentindex = e.currentTarget.dataset.parentindex;
      this.data.wenid.push(e.currentTarget.dataset.winid)
      // console.log(this.data.list[parentindex].datas)
      if (this.data.list[indexs][index].active) {
        this.data.list[indexs][index].active = false;
        this.setData({
          list: this.data.list,
          // ['list[' + index + '].datas[' + parentindex + '].active']: true,
          // distilleryid: e.currentTarget.dataset.winid,
          // wenid: this.data.wenid
        })
      } else {
        this.data.list[indexs][index].active = true;
        console.log(this.data.list)
        this.setData({
          list: this.data.list,
          // ['list[' + index + '].datas[' + parentindex + '].active']: true,
          distilleryid: e.currentTarget.dataset.winid,
          wenid: this.data.wenid
        })
      }

    } else {
      // let data = e.currentTarget.dataset.item
      // console.log(data)
      // data.typeId = this.data.activeTab
      // if (data.typeId >= 1) {
      //   // wx.setStorageSync('data', data)
      //   console.log(JSON.stringify(data))
      //   wx.navigateTo({
      //     url: '/pages/details/index?id=' + JSON.stringify(data),
      //     success: function (res) {
      //       // 通过eventChannel向被打开页面传送数据
      //       // res.eventChannel.emit('acceptDataFromOpenerPage', data)
      //     }
      //   })
      // } else {
      //   console.log(e.currentTarget.dataset.item.wineid)

      wx.navigateTo({
        url: '/pages/works/index?id=' + e.currentTarget.dataset.id + '&type=1',
      })
      // }
    }
  },
  butt: function () {

    let yy = ''
    this.data.wenid.map(item => {
      if (yy == '') {
        yy += item
      } else {
        yy += `,${item}`
      }
    })
    console.log(yy)
    app.request({
      url: '/addGradevin',
      data:{
        wineids : yy
      },
      method:"post",
      success: function (res) {
        console.log(res)
        if (res.code == 200) {
          wx.showToast({
            title: '添加成功',
            icon: 'succes',
          })
          setTimeout(function () {
            wx.navigateTo({
              url: '../collect/collect',
            })
          }, 2000)


        } else {
          wx.showToast({
            title: '您已经添加过了',
            icon: 'succes',
          })

        }
      }

    })
  },
  handlerAlphaTap(e) {
    let {
      ap
    } = e.target.dataset;
    this.setData({
      alpha: ap
    });
  },
  handlerMove(e) {
    let {
      list
    } = this.data;
    let moveY = e.touches[0].clientY;
    let rY = moveY - this.offsetTop;
    if (rY >= 0) {
      let index = Math.ceil((rY - this.apHeight) / this.apHeight);
      if (0 <= index < list.length) {
        let nonwAp = list[index];
        nonwAp && this.setData({
          alpha: nonwAp.alphabet
        });
      }
    }
  },
  filterData(e) {
    let name = e.currentTarget.dataset.country
    let index = e.currentTarget.dataset.index
    console.log(index)

    //切换后先还原数据
    this.data.list = this.data.countryTabData

    this.setData({
      countryIndex: index
    })
    if (name == '全部') {
      this.setData({
        list: this.data.list
      })
      return
    }

    var data = JSON.parse(JSON.stringify(this.data.list)); //深拷贝
    let datas = data.map(item => {
      let inData = item.datas.filter(itemb => {
        if (itemb.country == name) {
          return itemb
        }
      })
      item.datas = inData
      return item
    })
    console.log(datas)
    this.setData({
      list: datas
    })
  },
  scrolltoupper: function () {
    wx.pageScrollTo({
      scrollTop: 5
    })
    this.setData({
      index: 0, //起始页
      des: 50
    })
    if (this.data.showpin == 1) {
      this.getsearChdata()
    }
    if (this.data.showpin == 2) {
      this.getsearChdatas()
    }
  },
  scrolltolower: function () {

    this.setData({
      index: this.data.index + 50, //起始页
      des: this.data.des + 50
    })
    if (this.data.showpin == 1) {
      this.getsearChdata()
    }
    if (this.data.showpin == 2) {
      this.getsearChdatas()
    }
  },
  onPageScroll(e) {
    // this.getDatas()
    if (e.scrollTop == 0) {
      if (this.data.xia == 1) {
        this.setData({
          index: 0, //起始页
          des: 50
        })
        this.getData(this.data.typeId)
      } else {
        this.setData({
          index: 0, //起始页
          des: 50
        })
        this.getData(this.data.typeId)
      }
    }
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("触头")
  },
  /**
   * 页面上拉触底事件的处理函数
   */

  onReachBottom: function () {
    console.log(this.data)

    if (this.data.more == true) {
      this.getData(this.data.typeId)
    } else {

    }

  },

})